﻿using Konveyer.Core.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Konveyer.Core
{
    /// <summary>
    /// Provides a striongly typed widget, override OnInvalidRequest type to see 
    /// incorrect request types passed to this widget
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class TypedWidget<T> : IWidget
        where T: class,IRequest
    {

        public void DoWork(IRequest request)
        {
            var req = request as T;
            if (req == null)
            {
                OnInValidRequestType(request);
            }
            else
            {
                Do(req);
            }

        }

        protected abstract void Do(T request);


        protected virtual void OnInValidRequestType(IRequest request)
        {
            throw new InvalidCastException("Cannot cast type " + request.GetType().Name + " as " + typeof(T).Name);
        }
    }
}
